﻿namespace PlugNT.Common
{
    using System;
    using System.Text;

    public class Pager
    {
        public static string GetAjaxPageNumbers(int curPage, int allPage, string callback, int extendPage)
        {
            int startPage = 1;
            int endPage = 1;
            string t1 = "<a href=\"###\" onclick=\"" + string.Format(callback, "1");
            string t2 = "<a href=\"###\" onclick=\"" + string.Format(callback, allPage);
            t1 = t1 + "\">&laquo;</a>";
            t2 = t2 + "\">&raquo;</a>";
            if (allPage < 1)
            {
                allPage = 1;
            }
            if (extendPage < 3)
            {
                extendPage = 2;
            }
            if (allPage > extendPage)
            {
                if ((curPage - (extendPage / 2)) > 0)
                {
                    if ((curPage + (extendPage / 2)) < allPage)
                    {
                        startPage = curPage - (extendPage / 2);
                        endPage = (startPage + extendPage) - 1;
                    }
                    else
                    {
                        endPage = allPage;
                        startPage = (endPage - extendPage) + 1;
                        t2 = "";
                    }
                }
                else
                {
                    endPage = extendPage;
                    t1 = "";
                }
            }
            else
            {
                startPage = 1;
                endPage = allPage;
                t1 = "";
                t2 = "";
            }
            StringBuilder s = new StringBuilder("");
            s.Append(t1);
            for (int i = startPage; i <= endPage; i++)
            {
                if (i == curPage)
                {
                    s.Append("<span>");
                    s.Append(i);
                    s.Append("</span>");
                }
                else
                {
                    s.Append("<a href=\"###\" onclick=\"");
                    s.Append(string.Format(callback, i));
                    s.Append("\">");
                    s.Append(i);
                    s.Append("</a>");
                }
            }
            s.Append(t2);
            return s.ToString();
        }

        public static string Pagination(int recordCount, int pageSize, int currPage, string urlFormat, string cssClassName, bool showPageCount, bool showDataCount)
        {
            return Pagination(recordCount, pageSize, currPage, urlFormat, "首页", "上一页", "下一页", "末页", cssClassName, showPageCount, showDataCount);
        }

        public static string Pagination(int recordCount, int pageSize, int currPage, string urlFormat, string firstText, string prevText, string nextText, string lastText, string cssClassName, bool showPageCount, bool showDataCount)
        {
            int allPage = 0;
            int nextPage = 0;
            int prevPage = 0;
            int startcount = 0;
            int endcount = 0;
            StringBuilder currPagestr = new StringBuilder();
            if (currPage < 1)
            {
                currPage = 1;
            }
            if (pageSize != 0)
            {
                allPage = recordCount / pageSize;
                allPage = ((recordCount % pageSize) != 0) ? (allPage + 1) : allPage;
                allPage = (allPage == 0) ? 1 : allPage;
            }
            nextPage = currPage + 1;
            prevPage = currPage - 1;
            startcount = ((currPage + 5) > allPage) ? (allPage - 9) : (currPage - 4);
            endcount = (currPage < 5) ? 10 : (currPage + 5);
            if (startcount < 1)
            {
                startcount = 1;
            }
            if (allPage < endcount)
            {
                endcount = allPage;
            }
            bool showflag = false;
            if (showPageCount)
            {
                currPagestr.Append("共" + allPage + "页");
                showflag = true;
            }
            if (showDataCount)
            {
                if (showflag)
                {
                    currPagestr.Append("(" + recordCount.ToString() + ")");
                }
                else
                {
                    currPagestr.Append("共" + recordCount.ToString() + "条记录");
                    showflag = true;
                }
            }
            if (showflag)
            {
                currPagestr.Append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
            }
            if (currPage > 1)
            {
                currPagestr.Append("<a href=\"" + string.Format(urlFormat, "1") + "\">" + firstText + "</a>&nbsp;&nbsp;<a href=\"" + string.Format(urlFormat, prevPage) + "\">" + prevText + "</a>");
            }
            else
            {
                currPagestr.Append("<span title=\"已经是第一页了\">" + firstText + "</span> <span title=\"已经是第一页了\">" + prevText + "</span>");
            }
            for (int i = startcount; i <= endcount; i++)
            {
                if (currPage == i)
                {
                    currPagestr.Append("&nbsp;&nbsp;<font color=\"#ff0000\">" + i + "</font>");
                }
                else
                {
                    currPagestr.Append(string.Concat(new object[] { "&nbsp;&nbsp;<a href=\"", string.Format(urlFormat, i), "\">", i, "</a>" }));
                }
            }
            if (currPage != allPage)
            {
                currPagestr.Append("&nbsp;&nbsp;<a href=\"" + string.Format(urlFormat, nextPage) + "\">" + nextText + "</a>&nbsp;&nbsp;<a href=\"" + string.Format(urlFormat, allPage) + "\">" + lastText + "</a>");
            }
            else
            {
                currPagestr.Append(" <span title=\"已经是最后一页了\">" + nextText + "</span> <span title=\"已经是最后一页了\">" + lastText + "</span>");
            }
            return ("<div class=\"" + cssClassName + "\">" + currPagestr.ToString() + "</div>");
        }
    }
}

